package com.warm.middle.email;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

/**
 * @Date: 2019/11/26 9:29
 * @Author: LChuang
 * @Description:
 */
@RestController
@RequestMapping("/mail")
public class MailController {

    @Autowired
    private JavaMailSender mailSender;

    @RequestMapping("/simple")
    public void simple(){
        SimpleMailMessage msg = new SimpleMailMessage();
        msg.setFrom("warm_lc@126.com");
        msg.setTo("1479798547@qq.com");
        msg.setSubject("It's test send mail for springboot");
        msg.setText("Hello, 1479798547");
        mailSender.send(msg);
    }

    @RequestMapping("/att")
    public void att(){
        try {
            MimeMessage msg = mailSender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(msg, true);
            helper.setFrom("warm_lc@126.com");
            helper.setTo("1479798547@qq.com");
            helper.setSubject("It's test send mail for springboot");
            helper.setText("Hello, 1479798547");
            //helper.addAttachment("附件1", new File("附件"));
            mailSender.send(msg);
        } catch (MessagingException e) {
            e.printStackTrace();
        }
    }
}
